<template>
  <zeda-dialog
    v-model="visible"
    title="会商研判上报"
    width="600px"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="model" :rules="rules" label-width="120px" size="small">
      <!-- <el-form-item label="会商单位" prop="switchStatus">
        <el-select v-model="form.jobGroup" placeholder="请选择任务分组">
                <el-option
                  v-for="dict in dict.type.sys_job_group"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
      </el-form-item> -->
      <el-form-item label="实际能耗" prop="switchStatus">
        <el-input placeholder="请输入内容" v-model="input" clearable>
          <template slot="append">kwh</template>
        </el-input>
      </el-form-item>
      <el-form-item label="实际电费" prop="switchStatus">
        <el-input placeholder="请输入内容" v-model="input" clearable>
          <template slot="append">元/kwh</template>
        </el-input>
      </el-form-item>
      <el-form-item label="额度超支率" prop="switchStatus">
        <el-input placeholder="请输入内容" v-model="input" clearable>
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="预算超支率" prop="switchStatus">
        <el-input placeholder="请输入内容" v-model="input" clearable>
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="工作违规次数" prop="switchStatus">
        <el-input-number v-model="input" :min="1" :max="10"></el-input-number>
      </el-form-item>
      <el-form-item label="参加会议人数" prop="switchStatus">
        <el-input-number v-model="input" :min="1" :max="10"></el-input-number>
      </el-form-item>
      <el-form-item label="会议主持人" prop="switchStatus">
        <el-input placeholder="请输入内容" v-model="input" clearable>
        </el-input>
      </el-form-item>
      <el-form-item label="会商研判情况" prop="switchStatus">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="会商研判情况"
          v-model="input"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="存在问题" prop="switchStatus">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="会商研判情况"
          v-model="input"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="需专班协调" prop="switchStatus">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="会商研判情况"
          v-model="input"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="典型案例" prop="switchStatus">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="会商研判情况"
          v-model="input"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <form-button
      slot="footer"
      :loading="loading"
      @cancel="handleClose"
      @submit="submit"
    >
      <el-button v-if="id > 0" type="info" size="small" @click="remove"
        >删除</el-button
      >
    </form-button>
  </zeda-dialog>
</template>
<script>
import request from "@/utils/request";
import Title from "@/components/Title";
import FormButton from "@/components/FormButton";
export default {
  components: {
    Title,
    FormButton,
  },
  props: {
    id: { type: [Number, String], default: "" },
    visible: { type: Boolean, default: false },
  },
  data() {
    return {
      loading: false,
      model: {
        substation: "",
        identity: "",
        taxNum: "",
        code: "",
        switchStatus: "",
      },
      rules: {
        substation: [
          { required: true, message: "请选择分所", trigger: "change" },
        ],
        identity: [
          { required: true, message: "请填写身份唯一标识", trigger: "blur" },
        ],
        taxNum: [
          { required: true, message: "请填写企业税号", trigger: "blur" },
        ],
        code: [{ required: true, message: "请填写所属编号", trigger: "blur" }],
        switchStatus: [
          { required: true, message: "请选择一键开票类型", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    visible(value) {
      if (value && this.id > 0) {
        this.getInfo();
      } else {
        this.model = {
          substation: "",
          identity: "",
          taxNum: "",
          code: "",
          switchStatus: "",
        };
      }
    },
  },
  methods: {
    getInfo() {
      request({
        url: `/third/invoice/config/detail/${this.id}`,
        method: "get",
      }).then((data) => {
        this.model = data;
      });
    },
    remove() {
      request({
        url: `/third/invoice/config/delete/${this.id}`,
        method: "get",
      }).then(() => {
        this.handleClose(true);
      });
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.id > 0) {
            request({
              url: `/third/invoice/config/switchStatus/${this.id}`,
              method: "get",
              params: {
                switchStatus: this.model.switchStatus,
              },
            })
              .then(() => {
                this.loading = false;
                this.handleClose(true);
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            request({
              url: "/third/invoice/config/add",
              method: "post",
              data: this.model,
            })
              .then(() => {
                this.loading = false;
                this.handleClose(true);
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    handleClose(status) {
      this.$emit("update:visible", false);
      this.$emit("close", status === true ? status : false);
    },
  },
};
</script>
